package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.bean.Dish;
import com.example.reggie.bean.ShoppingCart;
import com.example.reggie.service.DishService;
import com.example.reggie.service.ShoppingCartService;
import com.example.reggie.utils.BaseContext;
import com.example.reggie.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private DishService dishService;

    @GetMapping("/list")
    public R<List> allList(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getId());
        List<ShoppingCart> list =shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @Transactional
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        Long userId=BaseContext.getId();
        Long disId=shoppingCart.getDishId();
        Long setMealId=shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (disId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,disId);
        }
        if (setMealId!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,setMealId);
        }
        ShoppingCart serviceOne=shoppingCartService.getOne(queryWrapper);

        if (serviceOne==null){
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else{
            serviceOne.setNumber(serviceOne.getNumber()+1);
            shoppingCartService.updateById(serviceOne);
        }

        return R.success("加入購物車成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getId());
        Long dishId=shoppingCart.getDishId();
        Long setmealId=shoppingCart.getSetmealId();
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else if(setmealId!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart serviceOne=shoppingCartService.getOne(queryWrapper);
        if (serviceOne.getNumber()==1){
            shoppingCartService.remove(queryWrapper);
        }else{
            serviceOne.setNumber(serviceOne.getNumber()-1);
            shoppingCartService.updateById(serviceOne);
        }
        return R.success("刪除成功");
    }

    @DeleteMapping("/clean")
    public R<String> delete(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        shoppingCartService.remove(queryWrapper);
        return R.success("刪除成功");
    }
}
